<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>MongoDB | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/61.fa71d884.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>NoSQL</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0a818f/" class="sidebar-link">Redis入门</a></li><li><a href="/pages/4c087b/" aria-current="page" class="active sidebar-link">MongoDB</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#二、认识mongodb" class="sidebar-link">二、认识MongoDB</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-1-nosql" class="sidebar-link">2.1 NoSQL</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-2-为什么是mongodb" class="sidebar-link">2.2 为什么是MongoDB</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-3-mongodb简介" class="sidebar-link">2.3 MongoDB简介</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-4-mongodb优势" class="sidebar-link">2.4 MongoDB优势</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-4-1-建模简单而直观" class="sidebar-link">2.4.1 建模简单而直观</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-4-2-建模灵活可拓展" class="sidebar-link">2.4.2 建模灵活可拓展</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-4-3-api自然-快速开发" class="sidebar-link">2.4.3 API自然，快速开发</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-4-4-原生的高可用与易扩展" class="sidebar-link">2.4.4 原生的高可用与易扩展</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-5-mongodb适用场景" class="sidebar-link">2.5 MongoDB适用场景</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-5-1-要不要选mongodb" class="sidebar-link">2.5.1 要不要选MongoDB</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-5-2-mongodb使用场景" class="sidebar-link">2.5.2 MongoDB使用场景</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_2-5-3-mongodb不使用的场景" class="sidebar-link">2.5.3 MongoDB不使用的场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_2-6-mongodb小结" class="sidebar-link">2.6 MongoDB小结</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#三、mongodb安装" class="sidebar-link">三、MongoDB安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_3-1-服务端" class="sidebar-link">3.1 服务端</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_3-1-1-方式1-安装版" class="sidebar-link">3.1.1 方式1：安装版</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_3-1-2-方式2-绿色版" class="sidebar-link">3.1.2 方式2：绿色版</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_3-2-客户端" class="sidebar-link">3.2 客户端</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_3-2-1-mongodb-shell" class="sidebar-link">3.2.1 MongoDB Shell</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_3-2-2-compass" class="sidebar-link">3.2.2 compass</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_3-2-3-navicat-for-mongodb" class="sidebar-link">3.2.3 Navicat for MongoDB</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#四、核心概念" class="sidebar-link">四、核心概念</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_4-1-概念" class="sidebar-link">4.1 概念</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_4-2-数据类型" class="sidebar-link">4.2 数据类型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#五、数据库与集合" class="sidebar-link">五、数据库与集合</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_5-1-mongodb-shell方式" class="sidebar-link">5.1 MongoDB Shell方式</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-1-1-创建数据库" class="sidebar-link">5.1.1 创建数据库</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-1-2-删除数据库" class="sidebar-link">5.1.2 删除数据库</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-1-3-创建集合" class="sidebar-link">5.1.3 创建集合</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-1-4-查看集合" class="sidebar-link">5.1.4 查看集合</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-1-5-删除集合" class="sidebar-link">5.1.5 删除集合</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_5-2-navicat方式" class="sidebar-link">5.2 Navicat方式</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-2-1-创建数据库" class="sidebar-link">5.2.1 创建数据库</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_5-2-2-创建集合" class="sidebar-link">5.2.2 创建集合</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#六、文档操作" class="sidebar-link">六、文档操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_6-1-文档添加" class="sidebar-link">6.1 文档添加</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-1-1-语法" class="sidebar-link">6.1.1 语法</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-1-2-注意" class="sidebar-link">6.1.2 注意</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-1-3-练习" class="sidebar-link">6.1.3 练习</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-1-4-查看" class="sidebar-link">6.1.4 查看</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_6-2-文档更新" class="sidebar-link">6.2 文档更新</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-2-1-语法" class="sidebar-link">6.2.1 语法</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-2-2-练习" class="sidebar-link">6.2.2 练习</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_6-3-文档删除" class="sidebar-link">6.3 文档删除</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-3-1-语法" class="sidebar-link">6.3.1 语法</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-3-2-练习" class="sidebar-link">6.3.2 练习</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_6-4-文档查询" class="sidebar-link">6.4 文档查询</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-4-1-数据准备" class="sidebar-link">6.4.1 数据准备</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-4-2-查询全部" class="sidebar-link">6.4.2 查询全部</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-4-3-排序" class="sidebar-link">6.4.3 排序</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-4-4-分页" class="sidebar-link">6.4.4 分页</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_6-5-条件查询" class="sidebar-link">6.5 条件查询</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-5-1-比较运算符" class="sidebar-link">6.5.1 比较运算符</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-5-2-逻辑运算符【拓展】" class="sidebar-link">6.5.2 逻辑运算符【拓展】</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_6-5-3-模糊查询【拓展】" class="sidebar-link">6.5.3 模糊查询【拓展】</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#七、文档设计" class="sidebar-link">七、文档设计</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_7-1-文档嵌套" class="sidebar-link">7.1 文档嵌套</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_7-2-数组方式" class="sidebar-link">7.2 数组方式</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#八、mongodb编程" class="sidebar-link">八、MongoDB编程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_8-1-基本crud" class="sidebar-link">8.1 基本CRUD</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_8-2-基本查询" class="sidebar-link">8.2 基本查询</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_8-2-1-jpa方式" class="sidebar-link">8.2.1 JPA方式</a></li><li class="sidebar-sub-header level4"><a href="/pages/4c087b/#_8-2-2-mongotemplate方式" class="sidebar-link">8.2.2 MongoTemplate方式</a></li><li class="sidebar-sub-header level3"><a href="/pages/4c087b/#_8-3-分页查询" class="sidebar-link">8.3 分页查询</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/4c087b/#九、小结与作业" class="sidebar-link">九、小结与作业</a></li></ul></li><li><a href="/pages/25c0d9/" class="sidebar-link">ElasticSearch</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=NoSQL" title="分类" data-v-06225672>NoSQL</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-12</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">MongoDB<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="关系型数据库-mongodb"><a href="#关系型数据库-mongodb" class="header-anchor">#</a> 关系型数据库-MongoDB</h1> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/MongoDB.jpg" alt="img"></p> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li><p>入门MongoDB</p></li> <li><p>了解MongoDB核心概念</p></li> <li><p>了解MongoDB数据库基本操作</p></li> <li><p>了解MongoDB集合基本操作</p></li> <li><p>了解MongoDB文档基本操作</p></li> <li><p>了解MongoDB基本查询操作</p></li> <li><p>了解MongoDB文档设计</p></li> <li><p>掌握SpringBoot集成MongoDB</p></li></ul> <h2 id="二、认识mongodb"><a href="#二、认识mongodb" class="header-anchor">#</a> 二、认识MongoDB</h2> <h3 id="_2-1-nosql"><a href="#_2-1-nosql" class="header-anchor">#</a> 2.1 NoSQL</h3> <p>NoSQL：Not Only SQL ,其本质还是一种数据库技术，但相比传统数据库它可以不会遵循一些约束，比如：sql标准、ACID属性，表结构等。</p> <p><strong>特点</strong></p> <ul><li>满足对数据库的高并发读写</li> <li>对海量数据的高效存储和访问</li> <li>对数据库高扩展性和高可用性</li> <li>灵活的数据结构，满足数据结构不固定的场景</li></ul> <p><strong>缺点</strong></p> <ul><li>一般不支持事务</li> <li>实现复杂SQL查询比较复杂</li> <li>运维人员数据维护门槛较高</li> <li>目前不是主流的数据库技术</li></ul> <p><strong>具体分类</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/catalog.png" alt="具体分类">*</p> <h3 id="_2-2-为什么是mongodb"><a href="#_2-2-为什么是mongodb" class="header-anchor">#</a> 2.2 为什么是MongoDB</h3> <p>权威数据库排行：https://db-engines.com/en/ranking</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902215937473.png" alt="image-20220902215937473"></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220039608.png" alt="image-20220902220039608"></p> <p>为啥是MongoDB？<strong>答：流行</strong></p> <h3 id="_2-3-mongodb简介"><a href="#_2-3-mongodb简介" class="header-anchor">#</a> 2.3 MongoDB简介</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220253433.png" alt="image-20220902220253433"></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220351192.png" alt="image-20220902220351192"></p> <h3 id="_2-4-mongodb优势"><a href="#_2-4-mongodb优势" class="header-anchor">#</a> 2.4 MongoDB优势</h3> <h4 id="_2-4-1-建模简单而直观"><a href="#_2-4-1-建模简单而直观" class="header-anchor">#</a> 2.4.1 建模简单而直观</h4> <p><strong>标准的关系型数据库</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902221044429.png" alt="image-20220902221044429">*</p> <p><strong>MongoDB数据库</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902221256906.png" alt="image-20220902221256906">*</p> <p><strong>采用bson结构存储, 建模方式自然而不失直观</strong></p> <h4 id="_2-4-2-建模灵活可拓展"><a href="#_2-4-2-建模灵活可拓展" class="header-anchor">#</a> 2.4.2 建模灵活可拓展</h4> <p><strong>1：多样性：同一个集合(表)异构数据(不同字段的文档对象)</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220754958.png" alt="image-20220902220754958">*</p> <p>2：动态性：需求变更，字段可更加需求动态拓展</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220821816.png" alt="image-20220902220821816">*</p> <p><strong>3：数据治理：支持Json Schema规范，限制字段类型，在保证模型的灵活性前提下，保证数据安全</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902220903929.png" alt="image-20220902220903929">*</p> <h4 id="_2-4-3-api自然-快速开发"><a href="#_2-4-3-api自然-快速开发" class="header-anchor">#</a> 2.4.3 API自然，快速开发</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902221315522.png" alt="image-20220902221315522"></p> <h4 id="_2-4-4-原生的高可用与易扩展"><a href="#_2-4-4-原生的高可用与易扩展" class="header-anchor">#</a> 2.4.4 原生的高可用与易扩展</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902221340066.png" alt="image-20220902221340066"></p> <h3 id="_2-5-mongodb适用场景"><a href="#_2-5-mongodb适用场景" class="header-anchor">#</a> 2.5 MongoDB适用场景</h3> <h4 id="_2-5-1-要不要选mongodb"><a href="#_2-5-1-要不要选mongodb" class="header-anchor">#</a> 2.5.1 要不要选MongoDB</h4> <p>虽说mongodb很好用，并不是一定要选，开发中需要综合考量（包括学习、开发、运维等成本）</p> <table><thead><tr><th><strong>应用特征</strong></th> <th><strong>Yes / No</strong></th></tr></thead> <tbody><tr><td>应用不需要事务及复杂 join 支持</td> <td>必须 Yes</td></tr> <tr><td>新应用，需求会变，数据模型无法确定，想快速迭代开发</td> <td>？</td></tr> <tr><td>应用需要2000-3000以上的读写QPS（更高也可以）</td> <td>？</td></tr> <tr><td>应用需要TB甚至 PB 级别数据存储</td> <td>?</td></tr> <tr><td>应用发展迅速，需要能快速水平扩展</td> <td>?</td></tr> <tr><td>应用要求存储的数据不丢失</td> <td>?</td></tr> <tr><td>应用需要99.999%高可用</td> <td>?</td></tr> <tr><td>应用需要大量的地理位置查询、文本查询</td> <td>？</td></tr></tbody></table> <p><strong>如果上述有1个 Yes，可以考虑 MongoDB，2个及以上的 Yes，选择MongoDB绝不会错</strong></p> <h4 id="_2-5-2-mongodb使用场景"><a href="#_2-5-2-mongodb使用场景" class="header-anchor">#</a> 2.5.2 MongoDB使用场景</h4> <p>MongoDB 的应用已经渗透到各个领域，比如游戏、物流、电商、内容管理、社交、物联网、视频直播等，以下是几个实际的应用案例</p> <ul><li>游戏场景，使用 MongoDB 存储游戏用户信息，用户的装备、积分等直接以内嵌文档的形式存储，方便查询、更新</li> <li>物流场景，使用 MongoDB 存储订单信息，订单状态在运送过程中会不断更新，以 MongoDB 内嵌数组的形式来存储，一次查询就能将订单所有的变更读取出来。</li> <li>社交场景，使用 MongoDB 存储存储用户信息，以及用户发表的朋友圈信息，通过地理位置索引实现附近的人、地点等功能</li> <li>物联网场景，使用 MongoDB 存储所有接入的智能设备信息，以及设备汇报的日志信息，并对这些信息进行多维度的分析</li> <li>视频直播，使用 MongoDB 存储用户信息、礼物信息等.</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902221646680.png" alt="image-20220902221646680">*</p> <h4 id="_2-5-3-mongodb不使用的场景"><a href="#_2-5-3-mongodb不使用的场景" class="header-anchor">#</a> 2.5.3 MongoDB不使用的场景</h4> <ul><li>对事务要求较高的系统：例如银行、财务、金融类等系统。MongoDB对事物的支持较弱；</li> <li>传统的对SQL要求较高的系统：特定问题的数据分析，多数据实体关联，</li> <li>涉及到复杂的、高度优化的查询方式；</li> <li>较为简单系统；数据结构相对固定，使用sql进行查询统计更加便利的时候；</li></ul> <h3 id="_2-6-mongodb小结"><a href="#_2-6-mongodb小结" class="header-anchor">#</a> 2.6 MongoDB小结</h3> <p>MongoDB：是一个数据库 ,高性能、无模式、文档性，目前nosql中最热门的数据库，开源产品，基于c++开发。是nosql数据库中功能最丰富，最像关系数据库的。</p> <p><strong>特点：</strong></p> <ul><li>面向集合文档的存储：适合存储Bson（json的扩展）形式的数据；</li> <li>格式自由，数据格式不固定，生产环境下修改结构都可以不影响程序运行；</li> <li>强大的查询语句，面向对象的查询语言，基本覆盖sql语言所有能力；</li> <li>完整的索引支持，支持查询计划；</li> <li>支持复制和自动故障转移；</li> <li>支持二进制数据及大型对象（文件）的高效存储；</li> <li>使用分片集群提升系统扩展性；</li> <li>使用内存映射存储引擎，把磁盘的IO操作转换成为内存的操作；</li></ul> <p>概括起来：</p> <p><strong>MongoDB能存：海量，不敏感，又要求一定查询性能的数据</strong></p> <h2 id="三、mongodb安装"><a href="#三、mongodb安装" class="header-anchor">#</a> 三、MongoDB安装</h2> <h3 id="_3-1-服务端"><a href="#_3-1-服务端" class="header-anchor">#</a> 3.1 服务端</h3> <p>下载：https://www.mongodb.com/download-center/community</p> <p>说明：Linux版的安装，这里不展开讲，后续讲Linux操作系统时再讲，此处重点讲Window版的安装</p> <p>Window安装版有2种方式：</p> <h4 id="_3-1-1-方式1-安装版"><a href="#_3-1-1-方式1-安装版" class="header-anchor">#</a> 3.1.1 方式1：安装版</h4> <p>下载后双击安装，然后一路next，next即可，小白首选。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902222949179.png" alt="image-20220902222949179">*</p> <h4 id="_3-1-2-方式2-绿色版"><a href="#_3-1-2-方式2-绿色版" class="header-anchor">#</a> 3.1.2 方式2：绿色版</h4> <p>下载后解压配置</p> <p><strong>步骤1：解压创建2个目录</strong></p> <p>在解压后的根目录下创建2个目录夹</p> <p>data/db：用于存放数据库信息</p> <p>logs：存放运行日志</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902223150532.png" alt="image-20220902223150532">*</p> <p><strong>步骤2：进入bin目录，配置配置文件：mongo.conf</strong></p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token comment">## 数据库文件路径</span>
<span class="token key attr-name">dbpath</span><span class="token punctuation">=</span><span class="token value attr-value">D:\mongodb-4.2.2\data\db</span>
<span class="token comment">## 日志文件</span>
<span class="token key attr-name">logpath</span><span class="token punctuation">=</span><span class="token value attr-value">D:\mongodb-4.2.2\logs\mongo.log</span>
<span class="token comment">## 日志采用追加模式，配置后mongodb日志会追加到现有的日志文件，不会重新创建一个新文件</span>
<span class="token key attr-name">logappend</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
<span class="token comment">## 启用日志文件，默认启用</span>
<span class="token key attr-name">journal</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
<span class="token comment">## 这个选项可以过滤掉一些无用的日志信息，若需要调试使用请设置为 false</span>
<span class="token key attr-name">quiet</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
<span class="token comment">## 端口号 默认为 27017</span>
<span class="token key attr-name">port</span><span class="token punctuation">=</span><span class="token value attr-value">27017</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>注意：重点修改dbpath  跟 logpath路径，分别指向步骤1中创建的目录。</p> <p><strong>步骤3：写一个启动脚本：statup.bat</strong></p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>mongod <span class="token parameter variable">--config</span> ./mongo.conf
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>步骤4：双击启动statup.bat命令</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220902223627434.png" alt="image-20220902223627434">*</p> <h3 id="_3-2-客户端"><a href="#_3-2-客户端" class="header-anchor">#</a> 3.2 客户端</h3> <p>MongoDB客户端有非常多选择：</p> <p>MongoVUE  RockMongo Studio 3T compass  Navicat for MongoDB  MongoDB shell</p> <h4 id="_3-2-1-mongodb-shell"><a href="#_3-2-1-mongodb-shell" class="header-anchor">#</a> 3.2.1 MongoDB Shell</h4> <p>MongoDB服务器自带客户端： MongoDB Shell</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904195129544.png" alt="image-20220904195129544">*</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904195153595.png" alt="image-20220904195153595"></p> <h4 id="_3-2-2-compass"><a href="#_3-2-2-compass" class="header-anchor">#</a> 3.2.2 compass</h4> <p>官方推荐：compass</p> <p>下载：https://www.mongodb.com/products/compass</p> <p>个人感觉不是很好用，反而Navicat for MongoDB这个相对好用，所以此处选择Navicat for MongoDB作为客户端</p> <h4 id="_3-2-3-navicat-for-mongodb"><a href="#_3-2-3-navicat-for-mongodb" class="header-anchor">#</a> 3.2.3 Navicat for MongoDB</h4> <p>下载：http://www.navicat.com.cn/products</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904192202770.png" alt="image-20220904192202770"></p> <p>Navicat目前企业版(可试用14天)最新是16，MongoDB支持至少12版本，大家根据自己的需要选择。最后强调：支持正版</p> <p>后续我们使用Navicat Premium 12版本，如果之前有安装过12 或者 12以上可以不用重新安装。</p> <p>安装步骤：略：无脑next，next，next。</p> <p>安装成功之后打开：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904192535616.png" alt="image-20220904192535616"></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904192654839.png" alt="image-20220904192654839">*</p> <p>创建连接成功之后，默认是看不了数据库的，需要点击查看：显式隐藏项目，才能看到MongoDB系统自带的数据库。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904192925853.png" alt="image-20220904192925853"></p> <p>到这，MongoDB安装算成功啦。</p> <h2 id="四、核心概念"><a href="#四、核心概念" class="header-anchor">#</a> 四、核心概念</h2> <p>对MongoDB有了大体了解之后，接下来就是MongoDB基本操作了。操作前希望时刻记得：MongoDB是一个数据库，那么它不会脱离数据库的几个核心概念：数据库，表，列，行。</p> <h3 id="_4-1-概念"><a href="#_4-1-概念" class="header-anchor">#</a> 4.1 概念</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904193330798.png" alt="image-20220904193330798">*</p> <p><strong>案例</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904193359686.png" alt="image-20220904193359686">*</p> <p>至于怎么定义数据库名， 集合名，字段名，跟之前MySQL定义数据库名，表名，列名遵循的规则一样</p> <h3 id="_4-2-数据类型"><a href="#_4-2-数据类型" class="header-anchor">#</a> 4.2 数据类型</h3> <p>MongoDB支持的数据类型有很多，不需要记忆所有，关注常用的即可</p> <table><thead><tr><th>数据类型</th> <th>描述</th> <th>举例</th></tr></thead> <tbody><tr><td>null</td> <td>表示空值或者未定义的对象</td> <td>{&quot;x&quot;: null}</td></tr> <tr><td>boolean</td> <td>true/false</td> <td>{&quot;x&quot;: true}</td></tr> <tr><td>int</td> <td>32位整数</td> <td>{&quot;x&quot;: NumberInt(&quot;3&quot;)}</td></tr> <tr><td>long</td> <td>64位整数</td> <td>{&quot;x&quot;: NumberLong(&quot;3&quot;)}</td></tr> <tr><td>double</td> <td>浮点数</td> <td>{&quot;x&quot;:3.14, &quot;y&quot;:3}</td></tr> <tr><td>string</td> <td>UTF-8字符串</td> <td>{&quot;x&quot;:&quot;dafei&quot;}</td></tr> <tr><td>_id</td> <td>12字节的唯一id，自动生成</td> <td>{&quot;_id&quot;:ObjectId(&quot;5e2ab4f48847000059006f73&quot;)}</td></tr> <tr><td>date</td> <td>从标准纪元开始的毫秒数</td> <td>{&quot;date&quot;:ISODate(&quot;2018-11-26T00:00:00.000Z&quot;)}</td></tr> <tr><td>Regular expression</td> <td>正则表达式，语法同js的正则对象</td> <td>{&quot;x&quot;: /foobar/i}</td></tr> <tr><td>code</td> <td>JavaScript代码块</td> <td>{&quot;x&quot;:function(){....}}</td></tr> <tr><td>undefined</td> <td>未定义</td> <td>{&quot;x&quot;:undefined}</td></tr> <tr><td>array</td> <td>值集合或者列表</td> <td>{&quot;x&quot;:[&quot;a&quot;,&quot;b&quot;]}</td></tr> <tr><td>object</td> <td>文档中嵌入另外一个文档</td> <td>{&quot;x&quot;:{&quot;a&quot;:1, &quot;b&quot;:2}}</td></tr></tbody></table> <h2 id="五、数据库与集合"><a href="#五、数据库与集合" class="header-anchor">#</a> 五、数据库与集合</h2> <p>官网操作文档：https://docs.mongodb.com/manual/introduction/</p> <h3 id="_5-1-mongodb-shell方式"><a href="#_5-1-mongodb-shell方式" class="header-anchor">#</a> 5.1 MongoDB Shell方式</h3> <h4 id="_5-1-1-创建数据库"><a href="#_5-1-1-创建数据库" class="header-anchor">#</a> 5.1.1 创建数据库</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904195129544.png" alt="image-20220904195129544">*</p> <p>MongoDB没有专门的创建数据库的命令， 可以使用use 来选择某个数据库， 如果库不存在， 将会创建，但是只有往该库加入文档后才保存成文件</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904195525644.png" alt="image-20220904195525644">*</p> <h4 id="_5-1-2-删除数据库"><a href="#_5-1-2-删除数据库" class="header-anchor">#</a> 5.1.2 删除数据库</h4> <p><strong>db.dropDatabase();</strong>    //删除当前所在数据库</p> <h4 id="_5-1-3-创建集合"><a href="#_5-1-3-创建集合" class="header-anchor">#</a> 5.1.3 创建集合</h4> <p>MongoDB中，不用创建集合， 因为没有固定的结构， 直接使用db.集合名称.命令 来操作就可以了， 如果非要显示创建的话使用：</p> <p><strong>db.createCollection(&quot;集合名称&quot;)</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904200014908.png" alt="image-20220904200014908">*</p> <p>​</p> <h4 id="_5-1-4-查看集合"><a href="#_5-1-4-查看集合" class="header-anchor">#</a> 5.1.4 查看集合</h4> <p><strong>show collections</strong>   或者   <strong>show tables</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904200033468.png" alt="image-20220904200033468">*</p> <h4 id="_5-1-5-删除集合"><a href="#_5-1-5-删除集合" class="header-anchor">#</a> 5.1.5 删除集合</h4> <p>语法：db.集合名.drop();</p> <p><strong>db.users.drop()</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904200210875.png" alt="image-20220904200210875">*</p> <h3 id="_5-2-navicat方式"><a href="#_5-2-navicat方式" class="header-anchor">#</a> 5.2 Navicat方式</h3> <h4 id="_5-2-1-创建数据库"><a href="#_5-2-1-创建数据库" class="header-anchor">#</a> 5.2.1 创建数据库</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904200414725.png" alt="image-20220904200414725"></p> <h4 id="_5-2-2-创建集合"><a href="#_5-2-2-创建集合" class="header-anchor">#</a> 5.2.2 创建集合</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904200933758.png" alt="image-20220904200933758"></p> <p>这里注意，MongoDB的集合最初没有任何字段，所以默认是一个空，里面都没有。</p> <h2 id="六、文档操作"><a href="#六、文档操作" class="header-anchor">#</a> 六、文档操作</h2> <p>为了开发方便， 建议使用Navicat的方式操作MongoDB命令。另外学习时，建议新建并保存一个查询文件，方便后续复习。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904201413919.png" alt="image-20220904201413919"></p> <blockquote><p><strong>题外话：</strong> MongoDB 语法使用JavaScript引擎进行解析的，如果你js学得还可以，那你就掌握了先手优势啦。</p></blockquote> <p>把港股创建的查询文件保存到桌面，结果是js后缀。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904201707555.png" alt="image-20220904201707555">*</p> <h3 id="_6-1-文档添加"><a href="#_6-1-文档添加" class="header-anchor">#</a> 6.1 文档添加</h3> <h4 id="_6-1-1-语法"><a href="#_6-1-1-语法" class="header-anchor">#</a> 6.1.1 语法</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span> <span class="token punctuation">{</span>文档<span class="token punctuation">}</span> <span class="token punctuation">)</span>   		  <span class="token comment">//插入单个</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>文档<span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>文档<span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token punctuation">)</span>   <span class="token comment">//插入多个</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="_6-1-2-注意"><a href="#_6-1-2-注意" class="header-anchor">#</a> 6.1.2 注意</h4> <p>1:往集合中新增文档，当集合不存在时会自动先创建集合</p> <p>2:当操作成功时，集合会给文档生成一个**_id**字段，也可以自己指定</p> <h4 id="_6-1-3-练习"><a href="#_6-1-3-练习" class="header-anchor">#</a> 6.1.3 练习</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//添加一个用户： 用户(id:1, name:&quot;dafei&quot;, age:18)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;dafei&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//添加多个用户：用户(id:2, name:&quot;xiaofei&quot;, age:16)  用户(id:3, name:&quot;laofei&quot;, age:88)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">16</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">&quot;laofei&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">88</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_6-1-4-查看"><a href="#_6-1-4-查看" class="header-anchor">#</a> 6.1.4 查看</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904202255125.png" alt="image-20220904202255125">*</p> <p><strong>换种风格</strong></p> <p>分别为：网格视图， 树视图，JSON视图</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904202341190.png" alt="image-20220904202341190">*</p> <h3 id="_6-2-文档更新"><a href="#_6-2-文档更新" class="header-anchor">#</a> 6.2 文档更新</h3> <h4 id="_6-2-1-语法"><a href="#_6-2-1-语法" class="header-anchor">#</a> 6.2.1 语法</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//update users set xx=1 where xx = 1</span>
<span class="token comment">//更新单个</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">updateOne</span><span class="token punctuation">(</span>
    <span class="token punctuation">{</span>query<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>update<span class="token punctuation">}</span>
<span class="token punctuation">)</span>

<span class="token comment">//更新多个</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">updateMany</span><span class="token punctuation">(</span>
    <span class="token punctuation">{</span>query<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>update<span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>query</strong> : update的查询条件，类似sql update查询内where后面的。
<strong>update</strong> : update的对象和一些更新的操作符（如$,$inc...）等，也可以理解为sql update查询内set后面的</p> <h4 id="_6-2-2-练习"><a href="#_6-2-2-练习" class="header-anchor">#</a> 6.2.2 练习</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//把一个带有name=dafei的文档，修改其age值为30</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">updateOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;dafei&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//修改所有name=dafei的文档，修改其name=大飞，age=20</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">updateMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;dafei&quot;</span><span class="token punctuation">}</span>， <span class="token punctuation">{</span><span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;大飞&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//修改所有的文档，修改其name=xxx，age=10</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">updateMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span>， <span class="token punctuation">{</span><span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;xxx&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_6-3-文档删除"><a href="#_6-3-文档删除" class="header-anchor">#</a> 6.3 文档删除</h3> <h4 id="_6-3-1-语法"><a href="#_6-3-1-语法" class="header-anchor">#</a> 6.3.1 语法</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//删除1个：</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">deleteOne</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span>query<span class="token punctuation">}</span>
<span class="token punctuation">)</span>
<span class="token comment">//删除多个：</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">deleteMany</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span>query<span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="_6-3-2-练习"><a href="#_6-3-2-练习" class="header-anchor">#</a> 6.3.2 练习</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//删除_id=xxx的文档</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">deleteOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token function">ObjectId</span><span class="token punctuation">(</span><span class="token string">&quot;xxx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//删除所有带有name=dafei的文档</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">deleteMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;bunny&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//删除当前数据库中所有文档</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">deleteMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_6-4-文档查询"><a href="#_6-4-文档查询" class="header-anchor">#</a> 6.4 文档查询</h3> <h4 id="_6-4-1-数据准备"><a href="#_6-4-1-数据准备" class="header-anchor">#</a> 6.4.1 数据准备</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;zhang quan dan&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">33</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;zhang kun&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">26</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;zhang han yun&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">29</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;cai xv kun&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">29</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;jia nai liang&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;fu rong wang&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;wang da&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">31</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;da wang&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span><span class="token function">NumberLong</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span><span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span><span class="token function">NumberInt</span><span class="token punctuation">(</span><span class="token number">26</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="_6-4-2-查询全部"><a href="#_6-4-2-查询全部" class="header-anchor">#</a> 6.4.2 查询全部</h4> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//sql: select age, name  from table where ....</span>
db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span>query<span class="token punctuation">}</span>， <span class="token punctuation">{</span>projection<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>query</strong>： 是条件
<strong>projection</strong>： 列的投影，指定返回哪些列 _id默认返回 eg: {&quot;_id&quot;: 0}</p> <p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>查所有用户
<span class="token comment">//sql: select * from users;</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>   <span class="token comment">//{} 是条件</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//第二个{} 表示查询哪些列</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_6-4-3-排序"><a href="#_6-4-3-排序" class="header-anchor">#</a> 6.4.3 排序</h4> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>query， projection<span class="token punctuation">)</span><span class="token punctuation">.</span>
<span class="token function">sort</span><span class="token punctuation">(</span>  <span class="token punctuation">{</span>列：<span class="token number">1</span><span class="token punctuation">}</span>    <span class="token punctuation">)</span>      <span class="token comment">//正序</span>
<span class="token function">sort</span><span class="token punctuation">(</span>  <span class="token punctuation">{</span>列：<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span>   <span class="token punctuation">)</span>     <span class="token comment">//倒序</span>

多列排序
<span class="token function">sort</span><span class="token punctuation">(</span>  <span class="token punctuation">{</span>列<span class="token number">1</span><span class="token operator">:</span><span class="token number">1</span>， 列<span class="token number">2</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span>  <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//查询所有用户按年龄排序</span>
<span class="token comment">//sql: select * from users order by age desc|asc</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//正序</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//倒序</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//多列排序</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_6-4-4-分页"><a href="#_6-4-4-分页" class="header-anchor">#</a> 6.4.4 分页</h4> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>跟mysql中 limit ？ ？ 一一对应</p> <p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//分页查询所有用户</span>
<span class="token comment">//约定每页显示3条</span>
<span class="token comment">//sql: select * from users limit 0, 3   </span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>    <span class="token comment">//第一页</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>    <span class="token comment">//第二页</span>
<span class="token operator">...</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token punctuation">(</span>currentPage<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> pageSize<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span>pageSize<span class="token punctuation">)</span><span class="token comment">//第n页</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_6-5-条件查询"><a href="#_6-5-条件查询" class="header-anchor">#</a> 6.5 条件查询</h3> <h4 id="_6-5-1-比较运算符"><a href="#_6-5-1-比较运算符" class="header-anchor">#</a> 6.5.1 比较运算符</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904215317774.png" alt="image-20220904215317774">*</p> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合名<span class="token punctuation">.</span> <span class="token function">find</span><span class="token punctuation">(</span>     <span class="token punctuation">{</span>    <span class="token literal-property property">字段</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">比较操作符</span><span class="token operator">:</span> 值， <span class="token operator">...</span><span class="token punctuation">}</span>      <span class="token punctuation">}</span>       <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>比较操作符</th> <th>解释</th></tr></thead> <tbody><tr><td>$gt</td> <td>大于</td></tr> <tr><td>$lt</td> <td>小于</td></tr> <tr><td>$gte</td> <td>大于等于</td></tr> <tr><td>$lte</td> <td>小于等于</td></tr> <tr><td>$ne</td> <td>不等</td></tr> <tr><td>$in</td> <td>在...中</td></tr></tbody></table> <p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>查询age <span class="token operator">&gt;</span> <span class="token number">30</span>的用户
<span class="token comment">//sql: select * from users where age &gt; 300</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
  <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">$gt</span><span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span> 


查询名字为 dafei 或xiaofei用户
<span class="token comment">//sql： select * form users where name in(&quot;dafei&quot;, &quot;xiaofei&quot;)</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
  <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token punctuation">{</span>$<span class="token keyword">in</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h4 id="_6-5-2-逻辑运算符【拓展】"><a href="#_6-5-2-逻辑运算符【拓展】" class="header-anchor">#</a> 6.5.2 逻辑运算符【拓展】</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904231302111.png" alt="image-20220904231302111">*</p> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>   <span class="token punctuation">{</span><span class="token literal-property property">逻辑操作符</span><span class="token operator">:</span> <span class="token punctuation">[</span>条件<span class="token number">1</span>， 条件<span class="token number">2</span>， <span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">}</span>   <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>比较操作符</th> <th>解释</th></tr></thead> <tbody><tr><td>$and</td> <td>&amp;&amp;</td></tr> <tr><td>$or</td> <td>||</td></tr> <tr><td>$not</td> <td>！</td></tr></tbody></table> <p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//查年龄在28 到 30间的用户信息</span>
<span class="token comment">//sql:  select * from users where age &gt;= 28 and age &lt;=30</span>

db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token literal-property property">$gte</span><span class="token operator">:</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token literal-property property">$lte</span><span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>

db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span>
	    <span class="token literal-property property">$and</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token literal-property property">$gte</span><span class="token operator">:</span><span class="token number">28</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token literal-property property">$lte</span><span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
	 <span class="token punctuation">}</span>
<span class="token punctuation">)</span>


<span class="token comment">//查看年龄小于28或者年龄大于30用户信息</span>
<span class="token comment">//sql: select * from users where age &lt;28 or age &gt;30</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span><span class="token literal-property property">$or</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$lt</span><span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$gt</span><span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>

<span class="token comment">//查看年龄等于28或者等于30用户信息</span>
<span class="token comment">//sql: select * from users where age =28 or age =30</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span><span class="token literal-property property">$or</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">28</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h4 id="_6-5-3-模糊查询【拓展】"><a href="#_6-5-3-模糊查询【拓展】" class="header-anchor">#</a> 6.5.3 模糊查询【拓展】</h4> <p><strong>语法</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>集合<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">列</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$regex</span><span class="token operator">:</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">关键字</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//正则对象</span>
db<span class="token punctuation">.</span>集合<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">列</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$regex</span><span class="token operator">:</span> <span class="token string">&quot;关键字&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">//正则表达式</span>

<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xxx</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span>     <span class="token operator">--</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token operator">%</span>xxx<span class="token operator">%</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^xxx</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span> <span class="token operator">--</span><span class="token operator">-</span><span class="token operator">&gt;</span>xxx<span class="token operator">%</span>
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xxx$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span> <span class="token operator">--</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token operator">%</span>xxx
<span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xxx</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">}</span>   忽略大小写
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><strong>练习</strong></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//查询name带有fei的用户信息</span>
<span class="token comment">//sql： select * from users where name like '%fei%'</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
  <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$regex</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">fei</span><span class="token regex-delimiter">/</span></span> <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>

<span class="token comment">//查name中包含fei字样，并且年龄在28 到 30间的用户信息,</span>
<span class="token comment">//sql:  select * from users where name like '%fei%' and age &gt;= 28 and age &lt;=30</span>
db<span class="token punctuation">.</span>users<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>
   <span class="token punctuation">{</span><span class="token literal-property property">$and</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$regex</span><span class="token operator">:</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">fei</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">$gte</span><span class="token operator">:</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token literal-property property">$lte</span><span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="七、文档设计"><a href="#七、文档设计" class="header-anchor">#</a> 七、文档设计</h2> <p>MongoDB文档设计跟普通的关系型数据库表设计类型，但是涉及到关联关系时，需要额外处理。</p> <h3 id="_7-1-文档嵌套"><a href="#_7-1-文档嵌套" class="header-anchor">#</a> 7.1 文档嵌套</h3> <p><strong>一对一关系 与 多对一关系</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904232043314.png" alt="image-20220904232043314">*</p> <h3 id="_7-2-数组方式"><a href="#_7-2-数组方式" class="header-anchor">#</a> 7.2 数组方式</h3> <p>一对多关系与多对多关系</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904232120980.png" alt="image-20220904232120980"></p> <p><strong>操作技巧：关系型数据库实体对象toJsonString之后就是MongoDB的数据文档</strong></p> <h2 id="八、mongodb编程"><a href="#八、mongodb编程" class="header-anchor">#</a> 八、MongoDB编程</h2> <p>这里重点讲解，springboot集成MongoDB的操作</p> <h3 id="_8-1-基本crud"><a href="#_8-1-基本crud" class="header-anchor">#</a> 8.1 基本CRUD</h3> <p><strong>需求：完成员工的CRUD</strong></p> <p><strong>步骤1：建立项目mongo-demo</strong></p> <p><strong>步骤2：导入依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.4.3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>


<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>    
    
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token comment">&lt;!--spring boot data mongodb--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-mongodb<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p><strong>步骤3：配置mongodb url</strong></p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token comment">## application.properties</span>
<span class="token comment">## 配置数据库连接</span>
<span class="token comment">#格式: mongodb://账号:密码@ip:端口/数据库?认证数据库</span>
<span class="token comment">#spring.data.mongodb.uri=mongodb://root:admin@localhost/mongodemo?authSource=admin</span>
<span class="token key attr-name">spring.data.mongodb.uri</span><span class="token punctuation">=</span><span class="token value attr-value">mongodb://localhost/mongodemo</span>

<span class="token comment">## 配置MongoTemplate的执行日志</span>
<span class="token key attr-name">logging.level.org.springframework.data.mongodb.core</span><span class="token punctuation">=</span><span class="token value attr-value">debug</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>步骤3：编写domain实体类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@Document</span><span class="token punctuation">(</span><span class="token string">&quot;users&quot;</span><span class="token punctuation">)</span> <span class="token comment">//设置文档所在的集合</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span>
    <span class="token comment">//文档的id使用ObjectId类型来封装，并且贴上@Id注解，</span>
    <span class="token comment">// 自动映射为_id 自动封装ObjectId</span>
    <span class="token annotation punctuation">@Id</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> age<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> hobby <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>@Id 主键id，将id映射成集合中_id列</strong></p> <p><strong>步骤4：编写持久层接口</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * MongoDB自定义对象的持久层接口
 * 1&gt;定义接口继承MongoRepository
 * 2&gt;明确指定2个泛型
 *     泛型1：当前接口操作实体对象：User
 *     泛型2：当前接口操作实体对象对应主键属性类型：id
 */</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">UserMongoRepository</span> <span class="token keyword">extends</span> <span class="token class-name">MongoRepository</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>步骤5：编写service接口与实现类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IUserService</span> <span class="token punctuation">{</span>

    <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">User</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> <span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserServiceImpl</span>  <span class="token keyword">implements</span> <span class="token class-name">IUserService</span><span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">UserRepository</span> userRepository<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        userRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        userRepository<span class="token punctuation">.</span><span class="token function">deleteById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">User</span> user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        userRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">User</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> userRepository<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> <span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> userRepository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><p><strong>步骤6：CRUD测试</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserTest</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">IUserService</span> userService<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSave</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        userService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token string">&quot;5de507fca0852c6c7ebc1eac&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;dafei2222&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        userService<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testDelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        userService<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">&quot;5de507fca0852c6c7ebc1eac&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>userService<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;5de507fca0852c6c7ebc1eac&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>userService<span class="token punctuation">.</span><span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><h3 id="_8-2-基本查询"><a href="#_8-2-基本查询" class="header-anchor">#</a> 8.2 基本查询</h3> <p><strong>需求：查询name=dafei数据</strong></p> <h4 id="_8-2-1-jpa方式"><a href="#_8-2-1-jpa方式" class="header-anchor">#</a> 8.2.1 JPA方式</h4> <p>步骤1：创建QueryTest</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryTest</span> <span class="token punctuation">{</span>

    <span class="token comment">//需求：查询name = dafei用户信息</span>
    <span class="token comment">//MQL: db.users.find({name:&quot;dafei&quot;})</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">UserRepository</span> repository<span class="token punctuation">;</span>
   
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findByName</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>步骤2：修改UserRepository接口</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">UserRepository</span> <span class="token keyword">extends</span> <span class="token class-name">MongoRepository</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token comment">//通过name查询员工数据</span>
    <span class="token class-name">User</span> <span class="token function">findByName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>解析：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token comment">/**
 *  Spring-Data-mongodb 实现了JPA 语法规则，所有可以使用JPA查询方法定义规则来实现快速查询
 *
 *    JPA查询方法定义规则：
 *      前缀  + 操作符 + 属性/列
 *
 *      前缀： find  / query
 *      操作符： by and or lessthan like....
 *
 */</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220904233544276.png" alt="image-20220904233544276"></p> <h4 id="_8-2-2-mongotemplate方式"><a href="#_8-2-2-mongotemplate方式" class="header-anchor">#</a> 8.2.2 MongoTemplate方式</h4> <p>JPA查询语法针对简单条件查询还是可行，但是，如果涉及到高级查询（多条件查询）就无能为力， 怎么办呢？</p> <p>使用spring-data-mongodb 另外一套操作api---MongoTemplate</p> <p><strong>需求：查询name=dafei数据</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryTest</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">MongoTemplate</span> template<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testQuery2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        <span class="token comment">//类似： mybatyis-plus 条件构造器--Wrapper</span>
        <span class="token class-name">Criteria</span> criteria <span class="token operator">=</span> <span class="token class-name">Criteria</span><span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 创建查询对象----MQL语句抽象对象</span>
        <span class="token class-name">Query</span> query <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 添加限制条件</span>
        query<span class="token punctuation">.</span><span class="token function">addCriteria</span><span class="token punctuation">(</span>criteria<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> template<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>query<span class="token punctuation">,</span> <span class="token class-name">User</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string">&quot;users&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="_8-3-分页查询"><a href="#_8-3-分页查询" class="header-anchor">#</a> 8.3 分页查询</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">MongoTemplate</span> mongoTemplate<span class="token punctuation">;</span>

<span class="token comment">// 分页查询文档，显示第2页，每页显示3个，按照id升序排列</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testQuery1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建查询对象</span>
    <span class="token class-name">Query</span> query <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置分页信息</span>
    query<span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置排序规则</span>

    query<span class="token punctuation">.</span><span class="token keyword">with</span><span class="token punctuation">(</span><span class="token class-name">Sort</span><span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token class-name">Sort<span class="token punctuation">.</span>Direction</span><span class="token punctuation">.</span><span class="token constant">ASC</span><span class="token punctuation">,</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> mongoTemplate<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>query<span class="token punctuation">,</span> <span class="token class-name">User</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string">&quot;users&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>到这，整个MongoDB知识到就介绍。</p> <h2 id="九、小结与作业"><a href="#九、小结与作业" class="header-anchor">#</a> 九、小结与作业</h2> <ul><li><p>入门MongoDB</p></li> <li><p>了解MongoDB核心概念</p></li> <li><p>了解MongoDB数据库基本操作</p></li> <li><p>了解MongoDB集合基本操作</p></li> <li><p>了解MongoDB文档基本操作</p></li> <li><p>了解MongoDB基本查询操作</p></li> <li><p>了解MongoDB文档设计</p></li> <li><p>掌握SpringBoot集成MongoDB</p></li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=NoSQL" title="标签">#NoSQL</a><a href="/tags/?tag=MongoDB" title="标签">#MongoDB</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/0a818f/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Redis入门</div></a> <a href="/pages/25c0d9/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">ElasticSearch</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/0a818f/" class="prev">Redis入门</a></span> <span class="next"><a href="/pages/25c0d9/">ElasticSearch</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/61.fa71d884.js" defer></script>
  </body>
</html>
